package com.bottle.utils;

import org.springframework.util.DigestUtils;

public class MD5Utils {
    /**
     * Method name: md5 <BR>
     * Description: 加密密码 <BR>
     * Remark: <BR>
     * @param text 明文
     * @return  String 密文<BR>
     */
    public static String md5(String text){
        //加密后的字符串
        String encodeStr= DigestUtils.md5DigestAsHex(text.getBytes());
        return encodeStr;
    }

    /**
     * Method name: verify <BR>
     * Description: 验证密码是否正确 <BR>
     * Remark: <BR>
     * @param text 明文
     * @param md5 密文
     * @return  boolean 结果<BR>
     */
    public static boolean verify(String text, String md5){
        //根据传入的密钥进行验证
        String md5Text = md5(text);
        if(md5Text.equalsIgnoreCase(md5)){
            return true;
        }
        return false;
    }

    public static boolean verify_16Encrypt(String text, String md5){
        //根据传入的密钥进行验证
        String md5Text = md5_16Encrypt(text);
        if(md5Text.equalsIgnoreCase(md5)){
            return true;
        }
        return false;
    }
    public static String md5_16Encrypt(String text){
        StringBuffer sb = new StringBuffer(md5(text));
        String result  = sb.toString().substring(8, 24);
        return result;
    }

    public static String compareToEncyPass(String id1,String id2){
        StringBuffer sb = new StringBuffer();
        if(id1.compareTo(id2)>=0){//id1 大于id2
            sb.append(id2).append("-").append(id1);
        }else{
            sb.append(id1).append("-").append(id2);
        }
        System.out.println(sb);
        return md5_16Encrypt(sb.toString());
    }
    public static void main(String[] args) {
//        System.out.println(md5("123456"));
        System.out.println(md5_16Encrypt("5eb5254aaa9a911838d1b6ac"));
//        System.out.println(md5("49ba59abbe56e057"));
//        System.out.println(compareToEncyPass("123","1123"));
    }
}
